﻿define([
    "esri/layers/GraphicsLayer", "esri/PopupTemplate"
], function(GraphicsLayer, PopupTemplate) {
    var ExGraphicsLayer = GraphicsLayer.createSubclass({
        setData: function(mapView, layer) {
            var mapUtils = mapView.mapUtils;
            var me = this;
            var options = layer.layer || {};
            var data = options.data;
            var _geometryType = options.geometryType;
            if (!data) {
                if (typeof options.callback == 'function') {
                    options.callback(layer);
                };
                return
            };
            if (_geometryType) {
                switch (_geometryType) {
                    case mapUtils.Enum.GType.Point:
                        me.setPointData(mapView, mapUtils, layer);
                        break;
                    case mapUtils.Enum.GType.Polyline:
                        me.setPolylineData(mapView, mapUtils, layer);
                        break;
                    case mapUtils.Enum.GType.Extent:
                    case mapUtils.Enum.GType.Circle:
                    case mapUtils.Enum.GType.Polygon:
                        me.setPolygonData(mapView, mapUtils, layer);
                        break;
                    case mapUtils.Enum.GType.Text:
                        me.setTextData(mapView, mapUtils, layer);
                        break;
                };
            } else {
                data.map(function(item) {
                    _geometryType = item.geometryType || mapUtils.Enum.GType.Point;
                    var options = {
                        layer: $.extend({}, layer.layer)
                    };
                    options.layer.data = [item];
                    switch (_geometryType) {
                        case mapUtils.Enum.GType.Point:
                            me.setPointData(mapView, mapUtils, options);
                            break;
                        case mapUtils.Enum.GType.Polyline:
                            me.setPolylineData(mapView, mapUtils, options);
                            break;
                        case mapUtils.Enum.GType.Extent:
                        case mapUtils.Enum.GType.Circle:
                        case mapUtils.Enum.GType.Polygon:
                            me.setPolygonData(mapView, mapUtils, options);
                            break;
                        case mapUtils.Enum.GType.Text:
                            me.setTextData(mapView, mapUtils, options);
                            break;
                    };
                });
            };
        },
        setPointData: function(mapView, mapUtils, layer) {
            var me = this;
            var data = layer.layer.data || [];
            var options = layer.layer || {};
            var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
            var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();

            lngLatArray.map(function(lngLat, index) {
                var point = mapUtils.createPoint(mapView, lngLat);
                var symbol = lngLat[symbolFieldName] || options.symbol || 'common_point';

                mapUtils.getSymbol(mapView, symbol, function(symbol) {
                    var attributes = lngLat;
                    var graphic = mapUtils.createGraphic(mapView, {
                        geometry: point,
                        symbol: symbol,
                        attributes: attributes,

                    });
                    me.add(graphic);
                    if (index == lngLatArray.length - 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        };
                    };
                });
            });
        },

        setPolylineData: function(mapView, mapUtils, layer) {
            var me = this;
            var data = layer.layer.data || [];
            var options = layer.layer || {};

            var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();
            var pathsFieldName = options.pathsFieldName || 'paths';
            data.map(function(item, index) {
                if (pathsFieldName) {
                    item.paths = item[pathsFieldName];
                };

                var polyline = mapUtils.createPolyline(mapView, item);

                var symbol = item[symbolFieldName] || options.symbol || 'common_polyline';
                mapUtils.getSymbol(mapView, symbol, function(symbol) {
                    var attributes = item;
                    var graphic = mapUtils.createGraphic(mapView, {
                        geometry: polyline,
                        symbol: symbol,
                        attributes: attributes
                    });
                    me.add(graphic);
                    if (index == data.length - 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        };
                    };
                });
            })
        },
        setPolygonData: function(mapView, mapUtils, layer) {
            var me = this;
            var data = layer.layer.data || [];
            var options = layer.layer || {};
            var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();
            var ringsFieldName = options.ringsFieldName || 'rings';
            data.map(function(item, index) {
                if (ringsFieldName) {
                    item.rings = item[ringsFieldName]
                };

                var polygon = mapUtils.createPolygon(mapView, item);
                var symbol = item[symbolFieldName] || options.symbol || 'common_polygon';
                mapUtils.getSymbol(mapView, symbol, function(symbol) {
                    var attributes = item;
                    var graphic = mapUtils.createGraphic(mapView, {
                        geometry: polygon,
                        symbol: symbol,
                        attributes: attributes
                    });
                    me.add(graphic);
                    if (index == data.length - 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        };
                    };
                });
            })
        },
        setTextData: function(mapView, mapUtils, layer) {
            var me = this;
            var data = layer.layer.data || [];
            var options = layer.layer || {};
            var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
            var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();
            var textFieldName = options.textFieldName;
            lngLatArray.map(function(lngLat, index) {
                var point = mapUtils.createPoint(mapView, lngLat);
                var symbol = lngLat[symbolFieldName] || options.symbol || 'common_text';

                mapUtils.getSymbol(mapView, symbol, function(symbol) {
                    if (typeof symbol == 'object') {
                        symbol = mapUtils.$.extend({}, symbol);
                        if (lngLat[textFieldName] || lngLat[textFieldName] == 0) {
                            symbol.setText(lngLat[textFieldName]);
                            var attributes = lngLat;
                            var graphic = mapUtils.createGraphic(mapView, {
                                geometry: point,
                                symbol: symbol,
                                attributes: attributes
                            });
                            me.add(graphic);
                            if (index == lngLatArray.length - 1) {
                                if (typeof options.callback == 'function') {
                                    options.callback(layer);
                                };
                            };
                        };
                    };
                });
            });
        },
        getGraphicLocation: function(mapView, esriEvent, mapUtils) {
            var me = this;
            var location = null;

            var _geometryType = esriEvent.graphic.geometry.type;
            switch (_geometryType) {
                case mapUtils.Enum.GType.Point:
                    location = me.getPointGraphicLocation(mapView, esriEvent);
                    break;
                case mapUtils.Enum.GType.Polyline:
                    location = me.getPolylineGraphicLocation(mapView, esriEvent);
                    break;
                case mapUtils.Enum.GType.Extent:
                case mapUtils.Enum.GType.Circle:
                case mapUtils.Enum.GType.Polygon:
                    location = me.getPolygonGraphicLocation(mapView, esriEvent);
                    break;
                case mapUtils.Enum.GType.Text:
                    location = me.getTextGraphicLocation(mapView, esriEvent);
                    break;
            };
            return location;
        },
        getPointGraphicLocation: function(mapView, esriEvent) {
            if (!esriEvent || !esriEvent.graphic) {
                return;
            }
            var geometry = esriEvent.graphic.geometry || {};

            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        },
        getPolylineGraphicLocation: function(mapView, esriEvent) {
            if (!esriEvent || !esriEvent.mapPoint) {
                return;
            }
            var geometry = esriEvent.mapPoint || {};

            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        },
        getPolygonGraphicLocation: function(mapView, esriEvent) {
            if (!esriEvent || !esriEvent.mapPoint) {
                return;
            }
            var geometry = esriEvent.mapPoint || {};
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {}
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        },
        getTextGraphicLocation: function(mapView, esriEvent) {
            if (!esriEvent || !esriEvent.graphic) {
                return;
            }
            var geometry = esriEvent.graphic.geometry || {};

            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        }
    });
    return ExGraphicsLayer;
});